<?php 
class AdModelAdmin
{
	public function runPreActions()
	{
		global $commander;
		global $adc;
		switch($commander->getPostAction())
		{	
			case 'list_model':
				if($_POST['new_item_title'] != '')
				{ self::addAdModel($_POST['new_item_title']); }
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'am_models', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'am_models', 0); }
				if($_POST['del_item_id'] > 0)
				{ self::deleteAdModel($_POST['del_item_id']); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $db;
		global $commander;
		global $adc;
		
		switch ($_POST['action'])
		{
			case 'list_model':
				if((count($_POST['Id']) > 0) && ($adc->getAdModels()))
				{
					$i = 0;
					foreach($_POST['Id'] as $id)
					{
						if(isset($adc->AdModels[$id]))
						{
							if($_POST['Title'][$i] !== $adc->AdModels[$id]->getTitle())
							{ 
								$adc->AdModels[$id]->setTitle($commander->formatText($_POST['Title'][$i]));
								$adc->AdModels[$id]->setDataChanged(true);
							}
							if($_POST['BrandId'][$i] != $adc->AdModels[$id]->getBrandId())
							{ 
								$adc->AdModels[$id]->setBrandId($_POST['BrandId'][$i]);
								$adc->AdModels[$id]->setDataChanged(true);
							}
							if($_POST['Year'][$i] != $adc->AdModels[$id]->getYear())
							{ 
								$adc->AdModels[$id]->setYear($_POST['Year'][$i]);
								$adc->AdModels[$id]->setDataChanged(true);
							}
							if($_POST['YearEnd'][$i] != $adc->AdModels[$id]->getYearEnd())
							{ 
								$adc->AdModels[$id]->setYearEnd($_POST['YearEnd'][$i]);
								$adc->AdModels[$id]->setDataChanged(true);
							}
							$adc->AdModels[$id]->update();
						}
						$i++;
					} //End foreach
					if($_POST['selected'])
					{
						foreach($_POST['selected'] as $id)
						{
							if(isset($adc->AdModels[$id]))
							{
								if($_POST['activate'] == '-1')
								{
									$adc->AdModels[$id]->setActive(1);
									$commander->setTableActive($id, 'am_models', 1); 
								}
								if($_POST['deactivate'] == '-1')
								{
									$adc->AdModels[$id]->setActive(0);
									$commander->setTableActive($id, 'am_models', 0); 
								}
							}
						}
					}
				}
			break;
			//List-Editor group actions end
			
			//Editor AdModel actions
			case 'editor_model':
				if($adc->getCurrentAdModelId() > 0)
				{
					if(is_object($adc->getCurrentAdModel()))
					{
						if(stripslashes($_POST['Title']) !== $adc->CurrentAdModel->getTitle())
						{ 
							$adc->CurrentAdModel->setTitle($commander->formatText($_POST['Title'])); 
							$adc->CurrentAdModel->setDataChanged(true);
						}
						if(intval($_POST['BrandId']) != $adc->CurrentAdModel->getBrandId())
						{ 
							$adc->CurrentAdModel->setBrandId(intval($_POST['BrandId']));
							$adc->CurrentAdModel->setDataChanged(true);
						}
						if(intval($_POST['Year']) != $adc->CurrentAdModel->getYear())
						{ 
							$adc->CurrentAdModel->setYear(intval($_POST['Year']));
							$adc->CurrentAdModel->setDataChanged(true);
						}
						if(intval($_POST['YearEnd']) != $adc->CurrentAdModel->getYearEnd())
						{ 
							$adc->CurrentAdModel->setYearEnd(intval($_POST['YearEnd']));
							$adc->CurrentAdModel->setDataChanged(true);
						}
						$adc->CurrentAdModel->Update();
					}
				}
			break;
		}
	}
	
	function addAdModel($title)
	{
		global $commander;
		global $adc;
		if($adc->getCurrentBrandId() > 0)
		{
			$title = $commander->formatText($title);
			if($title != '')
			{
				$sql  = 'INSERT INTO '.DB_PERFIX.'am_models (`BrandId`, `Title`)'; 
				$sql .= 'VALUES ('.$adc->getCurrentBrandId().', "'.$title.'");';
				$result = mysql_query($sql);
			}
		}
	}
	
	function deleteAdModel($id)
	{	
		if($id > 0)
		{	
			$sql = 'UPDATE '.DB_PERFIX.'am_ads SET ModId=0 WHERE ModId='.intval($id).';';
			$result = mysql_query($sql);
			$sql = 'DELETE FROM '.DB_PERFIX.'am_models WHERE id='.intval($id).' LIMIT 1;';
			$result = mysql_query($sql);
			return;
		}
	}
}
?>